package com.publicize.controller;

import com.publicize.commom.commonutils.JwtUtils;
import com.publicize.commom.commonutils.Result;
import com.publicize.entity.User;
import com.publicize.entity.Vo.UserLogin;
import com.publicize.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author bs
 * @since 2021-03-04
 */
@Slf4j
@RestController
@RequestMapping("/proscenium/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;
    //用户登录
    @PostMapping("userLogin")
    public Result userLogin(@RequestBody UserLogin userLogin){

        String token=userService.userLogin(userLogin);
        return Result.ok().data("token",token);
    }
    //用户注册
    @PostMapping("userRegister")
    public Result userRegister(@RequestBody User user){
        userService.userRegister(user);
        return Result.ok();
    }
    //退出登录
    @PostMapping("quitUser")
    public Result quitUser(HttpServletRequest httpServletRequest){
        boolean removeToken = JwtUtils.removeToken(httpServletRequest);
        return removeToken==true?Result.ok():Result.error();
    }
    //修改用户资料
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user){
        boolean b = userService.updateById(user);
        return b==true?Result.ok(): Result.error();
    }
//展示资料
    @GetMapping("showUser")
    public Result showUser(HttpServletRequest request){
        String id=JwtUtils.getMemberIdByJwtToken(request);
        User user = userService.getById(id);
        return Result.ok().data("user",user);
    }
}

